package com.web.manager.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.mapper.PassageMapper;
import com.web.manager.pojo.Passage;
import com.web.manager.pojo.Result;
import com.web.manager.pojo.User;
import com.web.manager.service.PassageService;
import com.web.manager.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/passage")
public class PassageController {

    @Autowired
    private PassageService passageService;
    @Resource
    private PassageMapper passageMapper;




    @GetMapping("/submit/{publisher}")
    public Result getById(@PathVariable String publisher){

        List<Passage> deptList =passageMapper.getByPublisher(publisher);
        return Result.success(deptList);
    }


    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Passage passage){

        User currentUser = TokenUtils.getCurrentUser();
        passage.setPublisher(currentUser.getUsername());
        passage.setTime(DateUtil.now());
        passageService.save(passage);

        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Passage passage){


        passageService.updateById(passage);  //updateUser
        return Result.success();
    }



    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){



        passageService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        log.info(String.valueOf(ids));
        for (Integer id: ids) {


            passageService.removeById(id);

        }

          //batchDeleteUser
        return Result.success();
    }

    /*
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<User> userList = userService.list();  //selectAll
        return Result.success(userList);
    }

     */



    //根据id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){

        Passage passage= passageService.getById(id);   //selectById
        return Result.success(passage);
    }


    @GetMapping("/{id2}")  //简写 //只限get方式可用以请求
    public Passage selectById2(@PathVariable Integer id2) {
        //调用service查询部门数据
        return passageMapper.selectById2(id2);
    }

    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String title,
                               @RequestParam String type) {

        QueryWrapper<Passage> queryWrapper = new QueryWrapper<Passage>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(title),"title",title);
        queryWrapper.like(StrUtil.isNotBlank(type),"type",type);
        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<Passage> page = passageService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }
}


